#region License
//-----------------------------------------------------------------------------
// Copyright (c) 2008, Aaron MacDougall, Daniel Jeffery
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
//
// * Neither the name of Aaron MacDougall or Daniel Jeffery nor the names of its contributors may
//   be used to endorse or promote products derived from this software without
//   specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Agro2D;
#endregion
namespace Agro2DTestbed
{
    public class MFrameAnimation: FrameAnimation
    {
        private uint startFrameOffset = 0;

        public MFrameAnimation()
        {
        }

        [ImportAttribute] 
        public uint StartFrameOffset
        {
            get { return startFrameOffset; }
            set { startFrameOffset = value; }
        }

        /// <summary>
        /// Sets the current frame in the animation.
        /// </summary>
        /// <param name="frameNum">desired frame</param>
        /// <param name="spriteProperties"></param>
        public void ForceSetFrame(int frameNum, ref SpriteProperties spriteProperties)
        {
            Debug.Assert(frameNum >= 0);
            Debug.Assert(frameNum < this.sourceRectangles.Count);

            this.frameNum = frameNum;
            spriteProperties.SourceRectangle = this.sourceRectangles[frameNum];
            spriteProperties.Texture = this.texture;
        }


        public override void GenerateSourceRectangles(TextureReference textureReference, uint frameWidth, uint frameHeight, uint numFrames, uint numColumns)
        {
            Texture2D texture = TextureManager.Instance.GetTexture(textureReference);

            // Note: These are describing the whole texture rather than the frames used by this animation.
            Debug.Assert(frameWidth > 0);
            Debug.Assert(frameHeight > 0);
            Debug.Assert(numFrames > 0);
            Debug.Assert(numColumns > 0);
            Debug.Assert(numColumns * frameWidth <= texture.Width);
            //Debug.Assert((numFrames / numColumns) * frameHeight <= texture.Height);

            this.Texture = textureReference;

            uint x = 0;
            uint y = 0;
            int column = 0;
            for (int i = 0; i < StartFrameOffset+numFrames; i++)
            {
                if(i >= StartFrameOffset)
                    this.SourceRectangles.Add(new Rectangle((int)x, (int)y, (int)frameWidth, (int)frameHeight));

                x += frameWidth;
                column++;
                if (column == numColumns)
                {
                    x = 0;
                    column = 0;
                    y += frameHeight;
                }
            }
        }

        public override object Clone()
        {
            MFrameAnimation clonedAnimation = new MFrameAnimation();
            clonedAnimation.FPS = this.FPS;
            clonedAnimation.Loop = this.Loop;
            if(this.Texture != null)
                clonedAnimation.Texture = this.Texture;
            clonedAnimation.startFrameOffset = this.startFrameOffset;

            foreach(Rectangle rect in this.SourceRectangles)
                clonedAnimation.SourceRectangles.Add(rect);

            return clonedAnimation;
        }
    }
}
